package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orientation.commom.Result;
import com.orientation.entity.Role;
import com.orientation.service.RoleService;
import com.orientation.vo.RolePermVo;
import com.orientation.vo.RoleSidebarVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2021-10-11
 */
@Api(tags = "角色")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;

    @ApiOperation("添加")
    @PostMapping
    public ResponseEntity<Result> insertRole(@RequestBody Role role) {
        roleService.insertRole(role);
        return ResponseEntity.ok(new Result());
    }


    @ApiOperation("修改")
    @PutMapping
    public boolean update(@RequestBody Role role) {
        return roleService.updateById(role);
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable String id) {
        return roleService.removeById(id);
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public IPage<Role> findPage( @RequestParam(required = false) String name,
                               @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return roleService.page(new Page<>(pageNum, pageSize), Wrappers.<Role>lambdaQuery().like(Role::getName, name));
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Role findById(@PathVariable String id) {
        return roleService.getById(id);
    }

    @ApiOperation("保存角色权限")
    @PostMapping("/perm/{id}")
    public boolean updatePermById(@PathVariable String id, @RequestParam String permission) {
        return roleService.updatePermById(id, permission);
    }

    @ApiOperation("查询权限")
    @GetMapping("/perm/{id}")
    public List<RolePermVo> findPermById(@PathVariable String id) {
        return roleService.findPermById(id);
    }

    @ApiOperation("查询导航")
    @GetMapping("/sidebar/querySidebarById")
    public List<RoleSidebarVo> querySidebarById(@RequestParam(value = "id") String id) {
        List<RoleSidebarVo> roleSidebarVos=  roleService.findSidebarById(id);
        //过滤一级字系统
        // roleSidebarVos=  roleSidebarVos.stream().filter((RoleSidebarVo v)->!v.getIndex().equals(0)).collect(Collectors.toList());
        // return roleService.findSidebarById(id);
        return roleSidebarVos;
    }

    @ApiOperation("查询导航")
    @GetMapping("/sidebar/{id}")
    public List<RoleSidebarVo> findSidebarById(@PathVariable String id) {
        return roleService.findSidebarById(id);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public List<Role> findAll() {
        return roleService.list();
    }

    @ApiOperation("查询所有角色权限分配下拉框")
    @GetMapping("/noAdmin")
    public List<Role> findAllNoAdmin() {
         // return roleService.list(Wrappers.<Role>lambdaQuery().ne(Role::getSid, 1));
        return roleService.list();
    }

    @ApiOperation("所有角色")
    @GetMapping("/map")
    public Map<String, Role> findAllMap() {
        return roleService.list().stream().collect(Collectors.toMap(Role::getSid, r -> r));
    }

    // @ApiOperation("所有角色")
    // @GetMapping("/roleList")
    // public List<Role> roleList() {
    //     return roleService.list();
    // }

}

